#ifndef __SONAR_KS103_H
#define __SONAR_KS103_H

#include <string.h>
#include <stdio.h>


//IO¿Ú²Ù×÷ºê¶¨Òå
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) 
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))  
//IO¿ÚµØÖ·Ó³Éä
#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C 
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C 
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C 
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C 
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C 
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    

#define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808 
#define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08 
#define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008 
#define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408 
#define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808 
#define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08 
#define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08 

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //Êä³ö 
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //ÊäÈë 

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //Êä³ö 
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //ÊäÈë 
//IO·½ÏòÉèÖÃ
#define SDA_IN()  {GPIOB->CRH&=0XFFFFFF0F;GPIOB->CRH|=0x00000080;}
#define SDA_OUT() {GPIOB->CRH&=0XFFFFFF0F;GPIOB->CRH|=0x00000030;}


//IO²Ù×÷º¯Êý	 
#define IIC_SCL    PBout(8) //SCL
#define IIC_SDA    PBout(9) //SDA
#define READ_SDA   PBin(9)  //ÊäÈëSDA

#define KS103_ADDR1 0XD0
#define KS103_ADDR2 0XD2
#define KS103_ADDR3 0XD4


#define CONFIG_SONAR_KS103_CHANNEL_NUM        5
#define CONFIG_SONAR_KS103_TIMEOUT_MS         41//33      /**< Sonar detection timeout in ms. */
#define SONAR_LIMIT 300                         //320//根据文档，超声波距离小于350mm左右，波束角比较小

extern uint16_t sonar_ks103_data;


typedef struct _sonar_ks103_channel {
    uint8_t  state;                             /**< Sonar sensor state. */
    uint8_t  id;                                /**< Sonar smaple buffer id. */
    uint8_t  cnt;                               /**< Sonar smaple count. */
    // uint16_t sample[CONFIG_SONAR_SAMPLE_SIZE];  /**< Sonar sample buffer. */
    uint32_t ticks;                             /**< Sonar sample ticks. */
    uint16_t distance;                          /**< Sonar sampel result. */
} sonar_ks103_channel_t;

enum _sonar_ks103_state {
    SONAR_KS103_IDLE = 0,
    SONAR_KS103_INIT,
    SONAR_KS103_MEASURE,
    SONAR_KS103_DONE,
    SONAR_KS103_EXIT,
};


uint8_t KS103_ReadOneByte(uint8_t address, uint8_t reg);
void KS103_WriteOneByte(uint8_t address,uint8_t reg,uint8_t command);
void IIC_Start(void);
void IIC_Stop(void);
uint8_t IIC_Wait_Ack(void);
void IIC_Ack(void);
void IIC_NAck(void);
void IIC_Send_Byte(uint8_t txd);
uint8_t IIC_Read_Byte(unsigned char ack);
void change_i2c_address(unsigned char addr_old, unsigned char addr_new);
uint16_t sonar_ks103_get(uint8_t ucI2cAddr);
void drv_sonar_ks103_init(void);


#endif
